package functions.ems_base_data;

import java.util.ArrayList;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.ClaimBean;
import com.beans.ProjectBean;

import coms.Constants;
import coms.JSONUtil;
import frame.HttpHelper;

public class Project {
	private org.slf4j.Logger logger = LoggerFactory.getLogger(Project.class);
	
	String result;
	ArrayList<ProjectBean> projectList;
	ProjectBean selectProject;
	
	public org.slf4j.Logger getLogger() {
		return logger;
	}
	public void setLogger(org.slf4j.Logger logger) {
		this.logger = logger;
	}
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public ArrayList<ProjectBean> getProjectList() {
		return projectList;
	}
	public void setProjectList(ArrayList<ProjectBean> projectList) {
		this.projectList = projectList;
	}
	public ProjectBean getSelectProject() {
		return selectProject;
	}
	public void setSelectProject(ProjectBean selectProject) {
		this.selectProject = selectProject;
	}
	
	/**
	 * 查询项目列表
	 * findPagedProjectListUrl="/ems-base-data/project/findPagedProjectList"
	 */
	public void findPagedProjectList(String ouCode) {
		String url = Constants.FIND_PAGED_PROJECT_LIST_URL;
	

		String jsonString = "{\"page\":{\"currentPage\":1,\"pageSize\":10,\"recordCount\":0},"
		    + "\"params\":{\"projectName\":\"\",\"projectCode\":\"\",\"isValidity\":\"Y\",\"isPost\":\"Y\",\"ouCode\":\""+ouCode+"\"},"
		        + "\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
		try {
		   result = HttpHelper.post(jsonString, url);
			logger.info("findPagedProjectList--------" + result);
			JSONObject object = JSONUtil.toJSON(result);
			projectList = new ArrayList<ProjectBean>();
			int total = object.getInt("total");
			JSONArray list = object.getJSONArray("list");
			for (int i = 0; i < list.length(); i++) {
				JSONObject projectObject = (JSONObject) list.get(i);

				ProjectBean projectbean = new ProjectBean(projectObject.getInt("projectId"), projectObject.getString("buCode"),
						projectObject.getString("projectCode"), projectObject.getString("projectName"),
						projectObject.getString("isValidity"), projectObject.getString("isPost"),
						projectObject.getString("isParentValue"), projectObject.getString("ouCode"));
				projectList.add(projectbean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		int size = projectList.size();
		if (size > 0) {
			Random dom = new Random(size);
			int i = dom.nextInt(size);
			selectProject = projectList.get(i);
		//	System.out.println("selectProject --- " + selectProject.toString());
		}
		logger.info("查询项目列表----------------end ------------" );
	}
}
